﻿using System;
using System.Collections;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Drawing.Charts;
using Sirenix.OdinInspector;
using Sirenix.Utilities;
using UnityEngine;
using DataTable = System.Data.DataTable;

namespace BearUtil
{
    /// <summary>
    /// 缓存数据
    /// </summary>
    public class ExcelData : ScriptableObject
    {
        [BoxGroup("基本属性"), LabelText("表名"), ReadOnly]
        public string sheetName;
        [BoxGroup("基本属性"), LabelText("列"), ReadOnly]
        public int col;
        [BoxGroup("基本属性"), LabelText("行"), ReadOnly]
        public int row;
        
        // 不能直接存储在 Asset 中，序列化不支持
        public string[,] Datas;
        
        // for save
        [SerializeField, HideInInspector]
        private string[] localData;

        public ExcelData()
        {
            sheetName = "";
            col = 0;
            row = 0;
        }

        public void Init(DataTable rawData)
        {
            sheetName = rawData.TableName;
            col = rawData.Columns.Count;
            row = rawData.Rows.Count;
            Datas = new string[col, row];
            localData = new string[col * row];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    Datas[x, y] = rawData.Rows[y][x].ToString();
                    localData[x * row + y] = Datas[x, y];
                }
            }
        }

        [BoxGroup("基本属性"), Button("编辑面板")]
        public void ShowEditView()
        {
            LoadTransformData();
            if (localData == null && Datas == null)
            {
                Debug.LogError("空数据");
                return;
            }

            ExcelViewWindow.OpenWindow(this);
        }

        // unity 序列化不支持 二维数组
        public void LoadTransformData()
        {
            if (localData == null || localData.Length <= 0)
                return;
            
            Datas = new string[col, row];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    Datas[x, y] = localData[x * row + y];
                }
            }
        }
    
        // 装配数据
        public void Assemble(Dictionary<Vector2Int, string> datas, int row, int col)
        {
            this.col = col;
            this.row = row;
            Datas = new string[col, row];
            localData = new string[col * row];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    Datas[x, y] = datas[new Vector2Int(x, y)];
                    localData[x * row + y] = Datas[x, y];
                }
            }
        }

        // 存储数据
        public void SaveData(string[,] newData)
        {
            localData = new string[col * row];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    localData[x * row + y] = newData[x, y];
                }
            }
        }
    }   
}